package com.caifu.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.caifu.mapper.CUserRecordMapper;
import com.caifu.mapper.DeviceMapper;
import com.caifu.pojo.CDevice;
import com.caifu.pojo.CUserRecord;
import com.caifu.pojo.SysUser;
import com.caifu.service.CUserRecordService;
import com.caifu.util.Util;
import com.caifu.util.Utils;
import com.caifu.vo.RecordVo;
import com.caifu.vo.UserRecordVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 * 人员进出记录表 服务实现类
 * </p>
 *
 * @author tww
 * @since 2020-12-14
 */
@Service
public class CUserRecordServiceImpl extends ServiceImpl<CUserRecordMapper, CUserRecord> implements CUserRecordService {

    @Resource
    private CUserRecordMapper cUserRecordMapper;
    @Resource
    private DeviceMapper deviceMapper;

    @Override
    public PageInfo<RecordVo> getRecordList(RecordVo recordVo, SysUser user) {

        HashMap<String, Object> map = new HashMap<>();
        map.put("userName", recordVo.getUserName());
        map.put("deptName", recordVo.getDeptName());
        map.put("userType", recordVo.getUserType());
        map.put("deviceName", recordVo.getDeviceName());
        if ("2".equals(user.getUserType())) {
            map.put("deptId", user.getRelativeId());
        } else if ("1".equals(user.getUserType())) {
            map.put("orgId", user.getRelativeId());
        }
        if (Utils.isNotNull(recordVo.getStartTime())) {
            String startTime = recordVo.getStartTime();
            String[] split = startTime.split("-");
            String date = "AND cur.create_time BETWEEN " + split[0] + " AND " + split[1];
            map.put("date", date);
        }
        PageHelper.startPage(recordVo.getCurrentPage(), recordVo.getPageSize());
        List<RecordVo> records = cUserRecordMapper.getRecordList(map);
        for (RecordVo record : records) {
            //格式时间
            record.setCreateTime(Util.stringDateToStringDate(record.getCreateTime(), "yyyyMMddHHmmss", "yyyy-MM-dd HH:mm:ss"));
            //类别
            if ("1".equals(record.getUserType())){
                record.setUserType("员工");
            }else{
                record.setUserType("访客");
            }
            //性别
            if ("1".equals(record.getUserSex())){
                record.setUserSex("男");
            }else {
                record.setUserSex("女");
            }
        }
        PageInfo<RecordVo> pageInfo = new PageInfo<>(records);
        return pageInfo;

    }

    @Override
    public Map addUserRecord(List<UserRecordVo> list) throws Exception {
        Map map = new HashMap();
        try {
            List<CUserRecord> rList = new ArrayList<>();
            for (UserRecordVo userRecordVo : list) {
                CUserRecord record = new CUserRecord();
                CDevice device = deviceMapper.getDeviceInfo(userRecordVo.getDeviceNo());
                record.setDeviceId(device.getId());
                record.setCreateTime(userRecordVo.getOpenDoorTime());
                record.setStatus("1");
                record.setType(userRecordVo.getStatus());
                record.setUserNo(userRecordVo.getUserNo());
                rList.add(record);
            }
            rList.forEach(model -> cUserRecordMapper.insert(model));
            map.put("code", "200");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("code", "301");
            throw new Exception();
        }
        return map;
    }

}
